Data Structures

What is Circular Queue in Data Structure? | Data Structure Tutorial

What is Circular Queue in Data Structure? | Data Structure Tutorial

What is Circular Queue in Data Structure? | Data Structure Tutorial

In this article, you will learn about:

  • What is a Circular Queue?
  • Working on the Circular Queue
  • Circular Queue Operations
  • Applications of Circular Queue

What is a Circular Queue?

In a circular queue, the last element of the queue is always pointed to the first element making a circular link. The main advantage of a circular queue over a simple queue is making better use of memory utilization. If the last position of the queue is full and the first position is empty, then we can insert an item in the first position. This particular thing is not possible in the simple queue. A circular queue is a kind of extended version of a regular queue where the last element is linked with the first element of the queue. It is forming a circle-like structure. Its circular queue helps in solving the major limitation of the simple queue. In a simple queue, after a bit of insertion and deletion, there is no usable space.

Working on the circular queue

Circular queue works by the approach of circular increment i.e when we try to insert the item in the queue it will increase the pointer and we reach the end of the queue, then we again start from the beginning of the queue. Here, the circular increment is done with the help of modulo division with the queue size i.e.

if REAR + 1 == 5 (overflow!),

REAR = (REAR +1)%5 ( It represents the start of the queue)

Circular Queue Operations:

The circular queue work in the following way:

  • Two pointers are FRONT and REAR.
  • FRONT is used to track the first element of the queue.
  • REAR is used to track the last element of the queue.
  • Initially, the value of FRONT and REAR is -1.

Enqueue Operation

  • Need to check if the queue is full or not. 
  • Set the value of the FRONT to 0 for the first element. 
  • Increase the value of the REAR by 1 (i.e if the rear reaches the end, then again it starts from the first index of the queue).
  • Add the item in the position pointed out by REAR.

Dequeue Operation

  • Need to check if the queue is empty. 
  • Return the value pointed by FRONT.
  • Removing the value will increase the FRONT index by 1. 
  • Reset the value of the FRONT and REAR to -1 for the last element.

Application of Circular Queue:

  • Memory Management
  • Traffic Management
  • CPU Scheduling
Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.